<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <p>{{doubleCount}}</p>
        <button @click="increase">+</button>
        <button @click="decrease">-</button>
    </div>
    <script src="vue.js"></script>
    <script src="vuex.js"></script>
    <script>
        const store = new Vuex.Store({
            state:{
                count:0
            },
            mutations:{//在此可以修改状态 redux.reducer
                add(state){//参数是老状态
                    // 可以直接修改老状态，因为老状态是响应式的，所以改变 完成页面会更新
                    state.count++;
                },
                minus(state){
                    state.count--;
                }
            }
        })
        var vm = new Vue({
            el:'#app',
            computed:{
                doubleCount(){
                    //console.log(store.state)
                    return store.state.count*2;
                }
            },
            methods:{
                increase(){
                    //向仓库提交一个mutation，变更状态
                    store.commit('add');
                },
                decrease(){
                    store.commit('minus');
                }
            }
        });
    </script>
</body>
</html>